package controller;

import java.sql.Timestamp;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import entity.Reply_suggest;
import entity.Suggest;
import service.SuggestService;

@Controller
public class SuggestController {
	@Resource(name="suggestServiceImpl")
	private SuggestService suggestService; 
	
	public SuggestService getSuggestService() {
		return suggestService;
	}

	public void setSuggestService(SuggestService suggestService) {
		this.suggestService = suggestService;
	}

	@RequestMapping("add_suggest")
	public ModelAndView addSuggest(HttpServletRequest request){
		String name = request.getParameter("name");
		if(name.equals("")||name.trim().equals("")){
			name="游客";
		}
		String email = request.getParameter("email");
		String website = request.getParameter("website");
		String review = request.getParameter("review");
		if(review.trim().equals("")){
			return new ModelAndView("redirect:suggest");
		}else{
			Suggest s = new Suggest();
			s.setEmail(email);
			s.setName(name);
			s.setPraise(0);
			s.setReview(review);
			s.setTime(new Timestamp(new Date().getTime()));
			s.setWebsite(website);
			suggestService.addSuggest(s);
			return new ModelAndView("redirect:suggest");
		}
	}
	
	@RequestMapping("reply_suggest")
	public ModelAndView replySuggest(HttpServletRequest request){
		int replyId = Integer.parseInt(request.getParameter("replyId"));
		String name = request.getParameter("name");
		if(name.equals("")||name.trim().equals("")){
			name="游客";
		}
		String email = request.getParameter("email");
		String website = request.getParameter("website");
		String review = request.getParameter("review");
		if(review.trim().equals("")){
			return new ModelAndView("redirect:suggest");
		}else{
			Reply_suggest rs = new Reply_suggest();
			rs.setEmail(email);
			rs.setName(name);
			rs.setReply_suggest_id(replyId);
			rs.setReview(review);
			rs.setTime(new Timestamp(new Date().getTime()));
			rs.setWebsite(website);
			suggestService.addReplySuggest(rs);
			return new ModelAndView("redirect:suggest");
		}
		
	}
}
